﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Util;
using System.Web.Mail;


namespace HGZP.PLL
{
    /// <summary>
    /// 给邮箱
    /// </summary>
    public class SendEmail
    {
        /// <summary>
        /// 邮件发送方法
        /// </summary>
        /// <param name="fromMail">发件人的地址</param>
        /// <param name="fromkey">发送人密码</param>
        /// <param name="toMail">收件人地址（可以可以以分号分隔的多人地址）</param>
        /// <param name="ccMail">接受抄送的邮件地址（可以可以以分号分隔的多人地址）</param>
        /// <param name="bccMail">接受匿名的邮件地址（可以可以以分号分隔的多人地址）</param>
        /// <param name="subject">主题</param>
        /// <param name="body">正文</param>
        /// <param name="ifhtml">邮件是否是html格式</param>
        /// <param name="upfile">附件</param>
        /// <returns>是否发送成功</returns>
        public static  bool SendMail(string fromMail, string fromkey, string toMail, string ccMail, string bccMail, string subject, string body, bool ifhtml, FileUpload upfile)
        {
            try
            {
                MailMessage myMail = new MailMessage();
                myMail.From = fromMail;//发件人的地址
                myMail.To = toMail;//收件人地址（可以可以以分号分隔的多人地址）
                myMail.Cc = ccMail;//接受抄送的邮件地址（可以可以以分号分隔的多人地址）
                myMail.Bcc = bccMail;//接受匿名的邮件地址（可以可以以分号分隔的多人地址）
                myMail.Subject = subject;//主题
                myMail.Body = body;//正文
                if (ifhtml)//正文内容类型
                {
                    myMail.BodyFormat = MailFormat.Html;
                }
                else
                {
                    myMail.BodyFormat = MailFormat.Text;
                }
                //附件
                string ServerFileName = "";
                if (upfile != null)
                {
                    if (upfile.PostedFile.ContentLength != 0)//判断附件大小
                    {
                        string upFileName = upfile.PostedFile.FileName;
                        string[] strTemp = upFileName.Split('.');
                        string upFileExp = strTemp[strTemp.Length - 1].ToString();
                        ServerFileName = System.Web.HttpContext.Current.Server.MapPath(DateTime.Now.ToString("yyyyMMddhhmmss") + "." + upFileExp);
                        upfile.PostedFile.SaveAs(ServerFileName);
                        myMail.Attachments.Add(new MailAttachment(ServerFileName));
                    }
                }

                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", fromMail); //发送方邮件帐户
                myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", fromkey); //发送方邮件密码

                SmtpMail.SmtpServer = "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1);
                SmtpMail.Send(myMail);

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}


